/**
 * components util
 */

/**
 * 清理空值，对象
 * @param children
 * @returns {*[]}
 */
import store from '@/store'
import { useStore } from 'vuex'
export function filterEmpty (children = []) {
  return children.filter(c => c.tag || (c.text && c.text.trim() !== ''))
}

/**
 * 获取字符串长度，英文字符 长度1，中文字符长度2
 * @param {*} str
 */
export const getStrFullLength = (str = '') =>
  str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128) {
      return pre + 1
    }
    return pre + 2
  }, 0)

/**
 * 截取字符串，根据 maxLength 截取后返回
 * @param {*} str
 * @param {*} maxLength
 */
export const cutStrByFullLength = (str = '', maxLength) => {
  let showLength = 0
  return str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128) {
      showLength += 1
    } else {
      showLength += 2
    }
    if (showLength <= maxLength) {
      return pre + cur
    }
    return pre
  }, '')
}
//动态加载多个js文件 
export const loadSomeJs = (arr,callback) => {
  //arr传 加载的js 文件
    if(arr&&arr.length>0){
      let length=arr.length
      var i=0
      load(i)
      // eslint-disable-next-line no-inner-declarations
      function load(){
        if(i<=length-1){
          let script = document.createElement('script')
          script.type = 'text/javascript'
          script.src =arr[i]
          document.getElementsByTagName('head')[0].appendChild(script)
          script.onload = function () {
            i++
            load()
            if(i==length){
              callback()
            }
          }
         
        }
      }
    }
  
}
export const initMapJs=(handle)=>{
  let jsArr=[
      '/static/bdmap/bmap_offline_api_v3.0_min.js',
      '/static/bdmap/DrawingManager_min.js',
      '/static/bdmap/BMapOperation.js',
      '/static/bdmap/GeoUtils_min.js',
    ]
  loadSomeJs(jsArr,handle)
}
